<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>分配业务员</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="${pageContext.request.contextPath}/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
<link href="${pageContext.request.contextPath}/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
<link href="${pageContext.request.contextPath}/css/animate.min.css" rel="stylesheet">
<link href="${pageContext.request.contextPath}/css/style.min.css?v=4.1.0" rel="stylesheet">
<link href="${pageContext.request.contextPath}/jQuery-Validation/css/validationEngine.jquery.css" rel="stylesheet">
<style type="text/css">
	.checkbox{padding:10px 0;}
	.checkbox input{margin-top:3px !important;}
</style>
</head>
<body>
<div class="container" style="margin-top: 20px">
	<form role="form" id="distributeDriverForm">
	     <div id="checkboxDiv">
				         
	     </div>
	     <div class="form-group" style="margin-left: 100px;margin-top: 100px">
	         <div class="col-sm-6  control-label">
	             <button type="button" class="btn btn-default" onclick="closeForm();">关闭</button>
	             &nbsp;&nbsp;&nbsp;&nbsp;<button type="submit" class="btn btn-primary" id="save">确定</button>
	         </div>
	     </div>
	</form>
</div>
<script src="${pageContext.request.contextPath}/js/jquery.min.js?v=2.1.4"></script>
<script src="${pageContext.request.contextPath}/js/bootstrap.min.js?v=3.3.6"></script>
<script src="${pageContext.request.contextPath}/js/plugins/layer/layer.min.js"></script>
<script src="${pageContext.request.contextPath}/jQuery-Validation/js/jquery.validationEngine.js"></script>
<script src="${pageContext.request.contextPath}/jQuery-Validation/js/jquery.validationEngine-zh_CN.js"></script>
<script src="${pageContext.request.contextPath}/js/public/ttbj-core.js"></script>

<script type="text/javascript">
var orderId = $.getUrlParam("id");
var require = $.getUrlParam("require");	//需要多少辆
var already = $.getUrlParam("already");	//已经分配了多少辆
$(function(){
	//ajax请求数据库获取司机的信息
    $.ajax({
        url: "../driver/getDriverAndComInfo.htm",
        cache: false,
        type: "get",
        dataType: "json",
        success: function (result) {
            if (result.status == 0) {
            	var num = require - already;	//还需几辆车
            	var checkboxElement = "";	//checkbox的html
            	var data = result.data;	//获取返回的数据
            	for(var i=0; i<data.length; i++){
            		checkboxElement += '<div class="checkbox"><label> <input type="checkbox" name="driver" class="validate[required,maxCheckbox[' + num +']]" value="'+data[i].driverId + '"/><span>' + data[i].driverRealName + '-' + data[i].truckCarnum  + '</span></lable></div>';
            	}
            	$("#checkboxDiv").empty().append(checkboxElement);
                return;
            }
        }
    });
	
  	//是否验证成功
    $('#distributeDriverForm').validationEngine('attach', {
        validationEventTrigger : 'blur',
        promptPosition: 'topRight',
        scroll: false,
        focusFirstField : true,
        onValidationComplete : function (form, valid) {
            if (valid){
            	adddistributeDriverInfo();
            }
        }
    });
});


//增加预订单分配司机信息
function adddistributeDriverInfo() {
	//数组对象
	var chk_value = getCheckBoxVal();
	console.log(JSON.stringify(chk_value));
    $.ajax({
        url: "../order/addedOrderWaybillInfo.htm",
        cache: false,
        type: "post",
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(chk_value),
        success: function (result) {
            if (result.status == 0) {
                parent.layer.msg(result.message, {icon: 1});
                parent.$driverOrderTable.bootstrapTable('refresh', {url: '../order/getAdvanceDriOrderInfoByPage.htm'});
                closeForm();
            } else {
                parent.layer.msg(result.message, {icon: 2});
                closeForm();
            }
        }
    });
}

//获取所有选中的复选框的值
function getCheckBoxVal(){
	var chk_value = [];
	//取出所有选中的checkbox
	$("#checkboxDiv").find("input[type='checkbox']").each(function(){
		if($(this).prop('checked') == true){
			var obj = {"waybillOrder":orderId, "waybillDriver":$(this).val(), "waybillTruck":$(this).next().text().split('-')[2]};
			chk_value.push(obj);
		}
	});
	return chk_value;
}

//关闭窗口
function closeForm() {
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}

</script>
</body>
</html>